package com.estronger.shopmanage.ui.othertask.presenter;

import com.estronger.shopmanage.ui.othertask.bean.OtherTaskDetailBean;
import com.estronger.shopmanage.ui.othertask.fragment.detail.DisposeDetailFragment;
import com.estronger.shopmanage.ui.othertask.fragment.detail.OtherTaskInfoFragment;
import com.google.gson.Gson;
import com.soonfor.evaluate.base.IEvalBasePresenter;
import com.soonfor.evaluate.bean.ReturnVisitBean;
import com.soonfor.evaluate.http.api.Request;
import com.soonfor.evaluate.http.httptools.AsyncUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.jesse.nativelogger.NLogger;

/**
 * 作者：DC-DingYG on 2018-10-16 19:25
 * 邮箱：dingyg012655@126.com
 */
public class DisposeDetailPresenter implements IEvalBasePresenter, AsyncUtils.AsyncCallback {

    private DisposeDetailFragment fragment;

    public DisposeDetailPresenter(DisposeDetailFragment fragment) {
        this.fragment = fragment;
    }

    //获取任务详情
    public void getTaskDetail(final String taskId) {
        if(OtherTaskInfoFragment.detailBean!=null){
            fragment.setDisposeView(true, OtherTaskInfoFragment.detailBean);
        }else {
            Request.OtherTask.getOtherTaskDetailInfo(fragment.getContext(), taskId, Request.OtherTask.GET_OTHERTASK_RESULT,this);
        }
    }
    //请求人工回访结果
    public void getQuestionnaire(String taskid){

        Request.Evaluate.getReturnVisitResult(fragment.getContext(), taskid, new AsyncUtils.AsyncCallback() {
            @Override
            public void success(int requestCode, String object) {
                Gson gson = new Gson();
                List<ReturnVisitBean> rvists = new ArrayList<>();
                //ReplyDataBean replyDataBean = null;
                String reVisitData = null, replayData = null;
                try {
                    JSONObject jo = new JSONObject(object);
                    reVisitData = jo.getString("rtnvisitdata");//回访数据集
                    //replayData = jo.getString("replydata");//回复
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if(reVisitData!=null && !reVisitData.equals("")){
                    try {
                        JSONArray ja = new JSONArray(reVisitData);
                        if (ja != null && ja.length() > 0) {
                            for (int i = 0; i < ja.length(); i++) {
                                String item = ja.getJSONObject(i).toString();
                                ReturnVisitBean visitBean = gson.fromJson(item, ReturnVisitBean.class);
                                if(visitBean.getFtype() == 1){
                                    List<ReturnVisitBean.Answer> answers = new ArrayList<>();
                                    ReturnVisitBean.Answer answer = new ReturnVisitBean.Answer("是", visitBean.getFpoint(), visitBean.getFynval());
                                    answers.add(answer);
                                    answer = new ReturnVisitBean.Answer("否", visitBean.getFpoint(), visitBean.getFynval()==0 ? 1:0);
                                    answers.add(answer);
                                    visitBean.setItem(answers);
                                }
                                rvists.add(visitBean);
                            }
                        }
                    }catch (Exception e){}
                }
                if (rvists.size()>0) {
                    fragment.setGetReturnVisit(true, rvists);
                }else {
                    fragment.setGetReturnVisit(false, rvists);
                }
            }
            @Override
            public void fail(int requestCode, int statusCode, String data, String msg) {
                fragment.setGetReturnVisit(false, null);
            }
        });
    }

    @Override
    public void success(int requestCode, String data) {
        Gson gson = new Gson();
        switch (requestCode) {
            case Request.OtherTask.GET_OTHERTASK_RESULT:
                try {
                    OtherTaskDetailBean dpBean = gson.fromJson(data, OtherTaskDetailBean.class);
                    if (dpBean != null) {
                        fragment.setDisposeView(true, dpBean);
                    } else
                        fragment.setDisposeView(false, null);
                } catch (Exception e) {
                    NLogger.e("解析结果详情报错：" + e.getMessage());
                }
                break;
        }
    }

    @Override
    public void fail(int requestCode, int statusCode, String data, String msg) {
        fragment.closeLoadingDialog();
        switch (requestCode) {
            case Request.OtherTask.GET_OTHERTASK_RESULT:
                fragment.setDisposeView(false, null);
                break;
            case Request.Evaluate.GET_QUESTIONNAIRE_RESULT://获取调查问卷结果
                fragment.setGetReturnVisit(false, null);
                break;
        }
    }
}
